package com.ship.system.service.impl;

import com.ship.common.business.service.impl.BaseServiceImpl;
import com.ship.common.core.utils.BeanCopyUtils;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.system.api.domain.SysUser;
import com.ship.system.domain.SysMessageConfig;
import com.ship.system.service.ISysUserService;
import com.ship.system.service.SysMessageConfigService;
import com.ship.system.mapper.SysMessageConfigMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author zhangyh
* @description 针对表【sys_message_config(系统消息配置信息表)】的数据库操作Service实现
* @createDate 2024-03-26 14:09:07
*/
@Service
public class SysMessageConfigServiceImpl extends BaseServiceImpl<SysMessageConfigMapper, SysMessageConfig>
    implements SysMessageConfigService{

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public JsonResult<SysMessageConfig> getDetail(Long configId) {
        SysMessageConfig messageConfig = this.baseMapper.selectById(configId);
        if(messageConfig == null){
            return JsonResult.failed("配置信息异常");
        }
        return JsonResult.success(messageConfig);
    }

    @Override
    public JsonResult configUpdate(SysMessageConfig config) {
        if(config.getId() == null){
            return JsonResult.failed("参数信息异常！");
        }
        SysMessageConfig messageConfig = this.baseMapper.selectById(config.getId());
        if(messageConfig == null){
            return JsonResult.failed("配置信息异常");
        }

        if(StringUtils.isNotBlank(config.getNoticeUids())){
            List<Long> uidList = Arrays.stream(config.getNoticeUids().split(","))
                    .map(Long::valueOf).collect(Collectors.toList());
            List<SysUser> userList = sysUserService.listByIds(uidList);
            if(uidList.size() != userList.size()){
                return JsonResult.failed("指定的通知人员信息异常，清重新指定！");
            }
            config.setNoticeUserNames(userList.stream().map(SysUser::getUserName).collect(Collectors.joining(",")));
        }else{
            config.setNoticeUserNames("");
        }

        BeanCopyUtils.copy(config,messageConfig);
        this.baseMapper.updateById(messageConfig);
        return JsonResult.success();
    }
}




